﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APPLICATION.Dict.Contracts
{
	public partial class frmContracts: Form
	{
		public frmContracts()
		{
			InitializeComponent();
		}

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            //splitContainer3.Panel2.Height = 100;
            //splitContainer3.Panel1.Height = splitContainer3.Height - splitContainer3.Panel2.Height;

            System.Data.SqlClient.SqlConnection cnSQL=new SqlConnection();
            System.Data.SqlClient.SqlDataAdapter daSQL=new SqlDataAdapter();
            System.Data.SqlClient.SqlCommand cmdSQL = new SqlCommand();

            if (cnSQL.State==ConnectionState.Open) {cnSQL.Close();}

            cnSQL.ConnectionString = globals.APPCONNSTRING;
            cmdSQL.CommandText = "tsp_Contracts";
            //cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 3;
            cmdSQL.CommandType = CommandType.StoredProcedure;
            cmdSQL.Connection = cnSQL;

            try
            {
                daSQL.SelectCommand = cmdSQL;
                cnSQL.Open();
                this.timsDS.tsp_Contracts.Clear();
                daSQL.Fill(this.timsDS.tsp_Contracts);
                cnSQL.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Ошибка при загрузке данных! \n"+ex.Message, "TIMS");
            }
            //cnSQL.ConnectionString = globals.APPCONNSTRING;
            
        }

        private void frmContracts_Shown(object sender, EventArgs e)
        {
            btnRefresh_Click(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddContract frmAdd=new frmAddContract("add");
            //frmAdd.btnChange.Visible = false;

            if (frmAdd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                btnRefresh_Click(sender,e);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //BindingSource bs=new BindingSource();
            if (timsDS.tsp_Contracts.Rows.Count>0)
            {
                frmAddContract frmEdit = new frmAddContract("edit");
                frmEdit.bs = this.tspContractsBindingSource;
                frmEdit.Text = "Редактирование контракта";
                //frmEdit.bs = (BindingSource) this.BindingContext;
                if (frmEdit.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    btnRefresh_Click(sender, e);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (timsDS.tsp_Contracts.Rows.Count>0)
            {
                DataRow rw = gridView1.GetDataRow(gridView1.FocusedRowHandle);
                DialogResult drs=new DialogResult();

                drs=MessageBox.Show("Are you sure you want to delete?", "TIMS", MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question);
                if (drs==System.Windows.Forms.DialogResult.Yes)
                {
                    System.Data.SqlClient.SqlCommand cmdSQL=new SqlCommand();
                    cmdSQL.CommandText = "tsp_Contracts";
                    cmdSQL.CommandType = CommandType.StoredProcedure;
                    cmdSQL.Parameters.Add("@cntID", SqlDbType.Int).Value = rw["cntID"];
                    cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 2;

                    System.Data.SqlClient.SqlConnection cnSQL=new SqlConnection();
                    cnSQL.ConnectionString = globals.APPCONNSTRING;

                    cmdSQL.Connection = cnSQL;
                    try
                    {
                        //MessageBox.Show("Deleting...");
                        cnSQL.Open();
                        cmdSQL.ExecuteNonQuery();
                        cnSQL.Close();
                        btnRefresh_Click(sender,e);
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show("Ошибка удаления записи! \n" + ex.Message, "TIMS");
                        throw;
                    }


                    //MessageBox.Show("Should be deleted...");
                }
                //gridView1.dat
                //MessageBox.Show("Are you sure you want to delete "+timsDS.tsp_Contracts.)
            }
        }

        private void gridControl1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.)
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyValue == 13)
                {
                    //gridView1.SelectRow(gridView1.GetSelectedRows()[0]+1);
                    //MessageBox.Show(gridView1.GetSelectedRows()[0].ToString());
                    if (gridView1.IsLastRow==false)
                    {
                        gridView1.FocusedRowHandle = gridView1.GetSelectedRows()[0] + 1;
                    }
                    else
                    {
                        gridView1.FocusedRowHandle = 0;
                    }
                    //gridView1.IsRowSelected(0)
                    //MessageBox.Show("Should select next "+gridView1.GetSelectedRows().ToString());
                    //gridView1.SelectRow(0);
                    //gridView1.SelectRow(gridView1.GetSelectedRows()[0] + 1);

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
	}
}
